package com.cloudcast.client;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cloudcast.client.controller.RootController;
import com.extjs.gxt.charts.client.Chart;
import com.extjs.gxt.charts.client.model.ChartModel;
import com.extjs.gxt.charts.client.model.LineDataProvider;
import com.extjs.gxt.charts.client.model.axis.XAxis;
import com.extjs.gxt.charts.client.model.axis.YAxis;
import com.extjs.gxt.charts.client.model.charts.LineChart;
import com.extjs.gxt.charts.client.model.charts.LineChart.LineStyle;
import com.extjs.gxt.charts.client.model.charts.dots.BaseDot;
import com.extjs.gxt.charts.client.model.charts.dots.Dot;
import com.extjs.gxt.themes.client.Slate;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.ThemeManager;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.Random;
import com.google.gwt.user.client.ui.RootPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class CloudCast implements EntryPoint {
	
	public void onModuleLoad() {
		ThemeManager.register(Slate.SLATE);
		
		HandlerManager eventBus = new HandlerManager(null);
		
		RootController rootController = new RootController(eventBus);
		
		ContentPanel rootContainer = new ContentPanel();
		rootContainer.setHeading("TEST");
		rootContainer.setWidth("100%");
		rootContainer.setHeight(400);
		rootContainer.setId("TEST_FULL_CONTAINER");
		rootContainer.setBorders(true);
		
		ContentPanel testContainer = new ContentPanel();
		testContainer.setHeading("TEST2");
		testContainer.setWidth("100%");
		testContainer.setHeight(300);
		testContainer.setBorders(false);
		
		rootContainer.add(getChart());
		
		RootPanel.get().add(rootContainer);		
		rootContainer.layout();
//		rootContainer.add(testContainer);	
//		rootContainer.layout();		
		
//		rootController.go(rootContainer);
	}
	
	private LayoutContainer getChart() {
		LayoutContainer container = new LayoutContainer();
		ChartModel model = new ChartModel();
		
		YAxis ya = new YAxis();
		ya.setMin(0);
		ya.setMax(12000);
		ya.setSteps(1500);
		model.setYAxis(ya);
		
		XAxis xa = new XAxis();			
		xa.setMin(0);
		xa.setMax(5);
		xa.setSteps(1);		
		model.setXAxis(xa);
				
		LineChart lineChart = new LineChart();		
		
		lineChart.addDots(getDots());
		model.addChartConfig(lineChart);
		
		lineChart.setAnimateOnShow(true);
//		LineStyle ls = new LineStyle(5, 2);
//		lineChart.setLineStyle(ls);
		
//		LineDataProvider provider = new LineDataProvider("xValue");
//		provider.bind(getStore());
//		lineChart.setDataProvider(provider);
		
		Chart chart = new Chart("/resources/chart/open-flash-chart.swf");
		chart.setChartModel(model);
		
		container.add(chart);
		
		return container;
	}
	
//	private ListStore<TestChart> getStore() {
//		ListStore<TestChart> testChart = new ListStore<TestChart>();
//		
//		testChart.add(new TestChart());
//		
//		return testChart;
//	}
	
	private List<BaseDot> getDots() {
		List<BaseDot> dots = new ArrayList<BaseDot>();
		
		for(Double i = 0d ; i <= 5 ; i += 0.01) {		
			Dot thisDot = new Dot();
			thisDot.setXY(i, 2000*i + Random.nextInt(100));
			dots.add(thisDot);
		}
		
		return dots;
	}
	
//	private class TestChart implements ModelData {
//
//		private Map<String, Object> values;
//		
//		public TestChart() {
//			values = new HashMap<String, Object>();
//			
//			for(int i=0 ; i<=5 ; i+=0.1)
//				values.put(String.valueOf(i), Random.nextInt(200));
//		}
//		
//		@Override
//		public <X> X get(String property) {
//			return (X)values.get(property);
//		}
//
//		@Override
//		public Map<String, Object> getProperties() {
//			return values;
//		}
//
//		@Override
//		public Collection<String> getPropertyNames() {
//			return values.keySet();
//		}
//
//		@Override
//		public <X> X remove(String property) {
//			return (X)values.remove(property);
//		}
//
//		@Override
//		public <X> X set(String property, X value) {
//			return (X) values.put(property, value);
//		}
//		
//	}
//	
}
